package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.mapper.RepairDetailMapper;
import com.indusfo.spc.mapper.RepairMapper;
import com.indusfo.spc.pojo.MatFlow;
import com.indusfo.spc.pojo.Repair;
import com.indusfo.spc.pojo.RepairDetail;
import com.indusfo.spc.service.RepairDetailService;
import com.indusfo.spc.vo.HintMessage;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RepairDetailServiceImpl implements RepairDetailService {

    private static final Logger logger = LoggerFactory.getLogger(RepairDetailServiceImpl.class);

    @Autowired
    private RepairDetailMapper repairDetailMapper;

    @Autowired
    private RepairMapper repairMapper;


    /***
     * 维修子工序查询（分页）
     *
     * @return com.indusfo.spc.vo.JSONObject
     */
    @Override
    public JSONObject selectAll(RepairDetail repairDetail) {
        List<RepairDetail> listRepairDetail = null;
        try {
            Integer pagesize = repairDetail.getPagesize();
            Integer pageindex = repairDetail.getPageindex();
            if (pagesize != null && pageindex != null) {
                repairDetail.setIncept(pagesize * (pageindex - 1));
            }
            Integer RepairId = repairDetail.getRepairId();
            if (RepairId == null) {
                List<Repair> Repairs = repairMapper.selectAll(null);
                repairDetail.setRepairId(Repairs.get(0).getRepairId());
            }
            //查询全部
            listRepairDetail = repairDetailMapper.selectAll(repairDetail);

            if (listRepairDetail.isEmpty()) {
                return JSONObject.oK("查询不到相关数据", listRepairDetail, 0);
            }
            //查询分页总记录数
            int count = repairDetailMapper.countRepairDetail(repairDetail); //返回查询到的总记录数
            return JSONObject.oK("查询成功", listRepairDetail, count);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 去添加查询（产品，任务单，批次）
     */
    @Override
    public JSONObject queryListRepairDetailToInsert(RepairDetail repairDetail) {
        try {
            //获取页面的数据个数
            Integer pagesize = repairDetail.getPagesize();
            //获取查询的页数
            Integer pageindex = repairDetail.getPageindex();
            if (pagesize != null && pageindex != null) {
                repairDetail.setIncept(pagesize * (pageindex - 1));
            }
            //执行查询操作
            List<RepairDetail> repairDetailList = repairDetailMapper.queryRepairDetailToInsert(repairDetail);

            //如果页数和页面数量都不为空的情况下进行计数
            if (repairDetailList.isEmpty()) {
                return JSONObject.oK(HintMessage.QUERY_NULL, repairDetailList, 0);
            } else {
                Integer count = repairDetailMapper.countRepairDetailToInsert(repairDetail);
                return JSONObject.oK(HintMessage.QUERY_SUCCESS, repairDetailList, count);
            }

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }


//    /**
//     * 维修子工序更新
//     *
//     * @param RepairDetail
//     * @return  JSONObject
//     */
//    @Override
//    public JSONObject updateRepairDetail(RepairDetail RepairDetail) {
//        String remark = RepairDetail.getRemark();
//        if(remark == null) {
//            remark = "";
//        }
//        try {
//            if(RepairDetail.getRepairDetailId() == null) {
//                throw new ParamsErrorException("请选择要更新的工序ID");
//            }
//            checkParam(RepairDetail, remark);
//            // 调用存储过程
//            int row = RepairDetailMapper.updateByPrimaryKeySelective(RepairDetail);
//            if(row == 0) {
//                throw new ModifyFailedException("数据更新失败");
//            }
//            return JSONObject.oK("更新成功");
//        } catch (GlobalException e) {  //这里只捕获自定义异常
//            logger.error(e.getMessage(), e);
//            return JSONObject.build(403, e.getMessage());
//        }
//    }
//
//    /***
//     * 维修子工序新增
//     *
//     * @param RepairDetail
//     * @return com.indusfo.spc.vo.JSONObject
//     */
//    @Override
//    public JSONObject insertRepairDetail(RepairDetail RepairDetail) {
//        RepairDetail.setDataState(1);
//        String remark = RepairDetail.getRemark();
//        if(remark == null) {
//            remark = "";
//        }
//        try {
//            if(RepairDetail.getRepairDetailId() != null) {
//                throw new ParamsErrorException("新增维修子工序时，不能填写id");
//            }
//            checkParam(RepairDetail, remark);
//            int	row = RepairDetailMapper.insertRepairDetail(RepairDetail);
//            if(row == 0) {
//                throw new ModifyFailedException("数据新增失败");
//            }
//            return JSONObject.oK("新增成功");
//        } catch (GlobalException e) {  //这里只捕获自定义异常
//            logger.error(e.getMessage(), e);
//            return JSONObject.build(403, e.getMessage());
//        }
//    }
//
//    /**
//     * 删除   维修子工序
//     *
//     * @param RepairDetailIds
//     * @param dataState
//     * @return com.indusfo.spc.vo.JSONObject
//     */
//    @Override
//    public JSONObject deleteBldc(Long[] RepairDetailIds, Integer dataState) {
//        dataState=2;
//        try {
//            if (RepairDetailIds == null) {
//                throw new ParamsErrorException("维修子工序id不能为空");
//            }
//            // 执行存储过程
//            int row = RepairDetailMapper.delteRepairDetail(RepairDetailIds,dataState);
//            String msg = "";
//            if (row == 0) {
//                new ModifyFailedException("删除失败");
//            } else {
//                msg = "删除成功";
//            }
//            return JSONObject.oK(msg);
//        } catch (GlobalException e) {
//            logger.error(e.getMessage(), e);
//            return JSONObject.build(403, e.getMessage());
//        }
//    }
//
//    /**
//     * 参数校验
//     *
//     * @param RepairDetail
//     * @param vcRemark
//     */
//    private void checkParam(RepairDetail RepairDetail, String vcRemark) {
//        if (StringUtils.isEmpty(RepairDetail.getAutoid())) {
//            throw new ParamsErrorException("请填写维修子工序名称");
//        }
//        if (RepairDetail.getAutoid().getBytes().length > 50) {
//            throw new ParamsErrorException("维修子工序名称超过规定长度");
//        }
//        if (vcRemark.getBytes().length > 100) {
//            throw new ParamsErrorException("说明字数超过规定长度");
//        }
//        // 名称不能重复
//        Integer depCounts = RepairDetailMapper.selectRepairDetail(RepairDetail);
//        if (depCounts !=0) {
//            throw new ParamsErrorException("该维修子工序已存在");
//        }
//    }
}
